StartPage = Class.create({
	initialize: function() {
        this.cookie = new Mojo.Model.Cookie("StartPagePreferences");
		this.url = "://www.startpage.com/do/metasearch.pl?language=english&cat=web&query=";
	},
	
	setSetting: function(field, value) {
		var settings = this.cookie.get();
		settings[field] = value;
		
		this.cookie.put(settings);
	},
	
	getSetting: function(field) {
		var settings = this.cookie.get();

		if (settings == undefined) {
			var settings = {};
			settings["https"] = "Yes";


	    	this.cookie.put(settings);
	    	var settings = this.cookie.get();
        }

        return settings[field];
    },

	search: function(keyword, callback) {
        if (this.getSetting("https") == "Yes")
        var httpsConnection = "https";
        else
        var httpsConnection = "http";

		new Ajax.Request(httpsConnection + this.url + keyword, {
			method: 'get',
			evalJSON: 'false',
			onSuccess: function(searchResult) {
				var responseText = searchResult.responseText;
				var urls = this._getUrls(responseText);

				callback(urls);
			}.bind(this),
		});
	},

	_getUrls: function(text) {
		var urls = new Array();

		var good = true;
		var i = 1;
		while (good == true) {
			var title = 'title_' + i;
			var url = this._getUrl(text, title);

			if (url != false)
			urls.push(url.toObject());
			else
			good = false;

			i++;
		}

		return urls;
	},

	_getUrl: function(fullText, title) {
		var ends = fullText.indexOf('id="' + title + '"') - 2;

		if (ends < 0) {
			return false;
		}
		var currentChar = ends;

		var continueSearch = true;
		while (continueSearch != false) {
			currentChar--;

			if (fullText[currentChar] == '"') {
				continueSearch = false;
			} else if (fullText[currentChar] == "\n\r") {
				continueSearch = false;
				return false;
			}
		}

		var result = $H();
		var url = fullText.substring(currentChar + 1, ends);
		result.set('url', url);
        result.set('domain', url.split(/\/+/g)[1]);

		// get title
		var restOfText = fullText.substring(currentChar, fullText.length);
		var titleStarts = restOfText.indexOf('">') + 2;
		var titleEnds = restOfText.indexOf('</a>');
		result.set('title', restOfText.substring(titleStarts, titleEnds).stripTags());

		// get description
		var restOfText = restOfText.substring(titleEnds, restOfText.length);
		var descriptionStarts = restOfText.indexOf('normaltext">') + 12;
		var descriptionEnds = restOfText.indexOf('<br>');
		result.set('description', restOfText.substring(descriptionStarts, descriptionEnds).stripTags());

		// get proxy
		var restOfText = restOfText.substring(descriptionEnds, restOfText.length);
		var currentChar = restOfText.indexOf('startpage-proxy.com');

		var continueSearch = true;
		while (continueSearch != false) {
			currentChar--;

			if (restOfText[currentChar] == '"') {
				continueSearch = false;
			} else if (restOfText[currentChar] == "\n\r") {
				continueSearch = false;
				return false;
			}
		}
		var proxyStarts = currentChar + 1;
		restOfText = restOfText.substring(proxyStarts, restOfText.length);

		var proxyEnds = restOfText.indexOf('"');
		result.set('proxy', restOfText.substring(0, proxyEnds).stripTags());

		return result;
	},
});
